﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DjDal;
using DjModel;

namespace DjBll
{
    public class Post
    {
        /// <summary>
        /// 得到分页文章列表
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="postsearch"></param>
        /// <returns></returns>
        public static IList<PostInfo> GetPostList(int pageindex, int pagesize, PostSearchInfo postsearch)
        {
            IList<PostInfo> postlist = null;
            try
            {
                using(DbHelper db = new DbHelper())
                {
                    postlist = DataProvider.GetPostList(db,pageindex,pagesize,postsearch);
                }
            }
            catch
            {
                postlist = null;
            }
            return postlist;
        }

        /// <summary>
        /// 得到具体文章
        /// </summary>
        /// <param name="postID"></param>
        /// <param name="penum"></param>
        /// <returns></returns>
        public static PostInfo GetPostInfo(int postID,PostGetEnum penum)
        {
            PostInfo post = null;
            if(postID>0)
            {
                try
                {
                    using(DbHelper db = new DbHelper())
                    {
                        post = DataProvider.GetPostByID(db,postID,penum);
                    }
                }
                catch
                {
                    post = null;
                }
            }
            return post;
        }

        /// <summary>
        /// 得到文章评论列表
        /// </summary>
        /// <param name="postid"></param>
        /// <returns></returns>
        public static IList<CommentInfo> GetPostCommentList(int postid)
        {
            IList<CommentInfo> commentlist = null;
            try
            {
                using (DbHelper db = new DbHelper())
                {
                    commentlist = DataProvider.GetPostComment(db, postid);
                }
            }
            catch
            {
                commentlist = null;
            }
            return commentlist;

        }
        /// <summary>
        /// 得到最新文章评论
        /// </summary>
        /// <param name="postid"></param>
        /// <returns></returns>
        public static IList<CommentInfo> GetLastCommentList(int num)
        {
            IList<CommentInfo> commentlist = null;
            try
            {
                using (DbHelper db = new DbHelper())
                {
                    commentlist = DataProvider.GetLastComment(db,num);
                }
            }
            catch
            {
                commentlist = null;
            }
            return commentlist;

        }

        /// <summary>
        /// 添加文章
        /// </summary>
        /// <param name="post"></param>
        /// <returns></returns>
        public static int AddPost(PostInfo post)
        {
            int ret = -1;
            try
            {
                using (DbHelper db = new DbHelper())
                {
                    post.ID = DataProvider.AddPost(db, post);
                    string[] term1;
                    if (!string.IsNullOrEmpty(post.CatString))
                    {
                        term1 = post.CatString.Split(',');
                        foreach (string s in term1)
                        {
                            DataProvider.AddTermRelation(db, Convert.ToInt32(s), post.ID);
                            DataProvider.UpdateTermPostNum(db, Convert.ToInt32(s));
                        }
                    }
                    string[] term2;
                    if (!string.IsNullOrEmpty(post.TagString))
                    {
                        term2 = post.TagString.Split(',');
                        foreach (string s in term2)
                        {
                            DataProvider.AddTermRelation(db, Convert.ToInt32(s), post.ID);
                            DataProvider.UpdateTermPostNum(db, Convert.ToInt32(s));
                        }
                    }
                    ret = 1;
                    DjBll.Term.ReloadDic();
                }
            }
            catch
            {
                ret = -1;
            }
            return ret;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="post"></param>
        /// <returns></returns>
        public static int UpdatePost(PostInfo post)
        {
            int ret = -1;
            try
            {
                using (DbHelper db = new DbHelper())
                {
                    DataProvider.UpdatePost(db, post);
                    ret = 1;
                    DjBll.Term.ReloadDic();
                }
            }
            catch
            {
                ret = -1;
            }
            return ret;
        }

        /// <summary>
        /// 添加文章评论
        /// </summary>
        /// <param name="comment"></param>
        /// <returns></returns>
        public static int AddComment(CommentInfo comment)
        {
            int ret = -1;
            try
            {
                using (DbHelper db = new DbHelper())
                {
                    DataProvider.AddPostComment(db, comment);
                    DataProvider.UpdatePostComment(db, comment.PostID);
                    ret = 1;
                }
            }
            catch
            {
                ret = -1;
            }
            return ret;
        }
        /// <summary>
        /// 更新文章阅读次数
        /// </summary>
        /// <param name="postid"></param>
        /// <returns></returns>
        public static int UpdateRead(int postid)
        {
            int ret = -1;
            try
            {
                using (DbHelper db = new DbHelper())
                {
                    DataProvider.UpdatePostRead(db,postid);
                    ret = 1;
                }
            }
            catch
            {
                ret = -1;
            }
            return ret;
        }
    }
}
